package com.note.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.note.app.annotation.LogRecord;
import com.note.app.dto.CollectionCreateDTO;
import com.note.app.dto.CollectionDTO;
import com.note.app.dto.CollectionQueryDTO;
import com.note.app.dto.CollectionUpdateDTO;
import com.note.app.service.CollectionService;
import com.note.common.context.UserContext;
import com.note.common.vo.ResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档集合控制器
 * 处理集合的CRUD操作
 */
@Slf4j
@RestController
@RequestMapping("/collection")
@Tag(name = "文档集合管理", description = "文档集合的增删改查接口")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    /**
     * 创建集合
     */
    @PostMapping
    @Operation(summary = "创建集合", description = "创建新的文档集合，可指定父级集合")
    @LogRecord(description = "创建文档集合", module = "文档集合管理", operationType = LogRecord.OperationType.CREATE)
    public ResponseVo<CollectionDTO> createCollection(@RequestBody @Valid CollectionCreateDTO createDTO) {
        Long userId = UserContext.getUserId();
        log.info("创建集合: userId={}, name={}, parentId={}", userId, createDTO.getName(), createDTO.getParentId());
        return collectionService.createCollection(userId, createDTO);
    }

    /**
     * 更新集合
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新集合", description = "更新集合信息，包括名称、描述和父级集合")
    @LogRecord(description = "更新文档集合", module = "文档集合管理", operationType = LogRecord.OperationType.UPDATE)
    public ResponseVo<CollectionDTO> updateCollection(
            @PathVariable("id") Long id,
            @RequestBody @Valid CollectionUpdateDTO updateDTO) {
        Long userId = UserContext.getUserId();
        updateDTO.setId(id);
        log.info("更新集合: userId={}, collectionId={}", userId, id);
        return collectionService.updateCollection(userId, updateDTO);
    }

    /**
     * 删除集合
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除集合", description = "删除指定的集合，如有子集合则不允许删除")
    @LogRecord(description = "删除文档集合", module = "文档集合管理", operationType = LogRecord.OperationType.DELETE)
    public ResponseVo<Void> deleteCollection(@PathVariable("id") Long id) {
        Long userId = UserContext.getUserId();
        log.info("删除集合: userId={}, collectionId={}", userId, id);
        return collectionService.deleteCollection(userId, id);
    }

    /**
     * 获取集合详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取集合详情", description = "获取指定集合的详细信息，包括子集合")
    @LogRecord(description = "获取文档集合详情", module = "文档集合管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<CollectionDTO> getCollection(@PathVariable("id") Long id) {
        Long userId = UserContext.getUserId();
        log.info("获取集合详情: userId={}, collectionId={}", userId, id);
        return collectionService.getCollection(userId, id);
    }

    /**
     * 分页查询集合列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询集合", description = "根据条件分页查询集合列表")
    @LogRecord(description = "分页查询文档集合", module = "文档集合管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<IPage<CollectionDTO>> pageCollections(
            @Parameter(description = "父级集合ID") @RequestParam(required = false) Long parentId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = UserContext.getUserId();
        
        CollectionQueryDTO queryDTO = new CollectionQueryDTO();
        queryDTO.setParentId(parentId);
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        
        log.info("分页查询集合: userId={}, parentId={}, pageNum={}, pageSize={}", 
                userId, parentId, pageNum, pageSize);
        
        return collectionService.pageCollections(userId, queryDTO);
    }

    /**
     * 获取集合树形结构
     */
    @GetMapping("/tree")
    @Operation(summary = "获取集合树", description = "获取当前用户的集合树形结构")
    @LogRecord(description = "获取文档集合树", module = "文档集合管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<List<CollectionDTO>> getCollectionTree() {
        Long userId = UserContext.getUserId();
        log.info("获取集合树: userId={}", userId);
        return collectionService.getCollectionTree(userId);
    }
} 